import common from '@ohos.app.ability.common';

/**
 * https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/reference/apis/js-apis-inner-application-eventHub.md/
 * 对EventHub的封装
 */
class EventHub {
  private hub: common.EventHub = null

  private static instance: EventHub = null;

  private constructor(hub: common.EventHub) {
    this.hub = hub;
  }

  static initEventHub(hub: common.EventHub) {

    EventHub.instance = new EventHub(hub);
  }

  static getInstance(): EventHub {
    if (!this.instance) {
      console.error('EventHub not init 请从EntryAbility 调用 EventHub.initEventHub(this.context.eventHub) ');
    }
    return this.instance;
  }


  public on(event: string, listener: Function) {
    this.hub?.on(event, listener);
  }

  public off(event: string, listener?: Function) {
    this.hub?.off(event, listener);
  }

  public emit(event: string, ...args: Object[]) {
    this.hub?.emit(event, args);
  }
}


export default EventHub;